package cn.wolfcode.crm.utils;

import cn.wolfcode.crm.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContext {
    public static final String EMPLOYEE_IN_SESSION="EMPLOYEE_IN_SESSION";

    public static HttpServletRequest getRequest(){
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public  static HttpSession  getSession(){
        return getRequest().getSession();
    }
        //存入 登录的用户的信息 session
    public static void setEmployee(Employee employee){
        HttpSession session = getSession();
        session.setAttribute(EMPLOYEE_IN_SESSION,employee);
    }
        //取出 登录用户的信息
    public static  Employee getEmployee(){
        HttpSession session=getSession();
        return (Employee) session.getAttribute(EMPLOYEE_IN_SESSION);
    }

    //存取用户权限
    public static final String EXPRESSIONS_IN_SESSION="EXPRESSIONS_IN_SESSION";

    //存入 登录的用户的权限信息 session
    public static void setExpressions(List<String> permissionExpressions){
        HttpSession session = getSession();
        session.setAttribute(EXPRESSIONS_IN_SESSION,permissionExpressions);
    }
    //取出 登录用户的权限信息
    public static  List<String> getExpressions(){
        HttpSession session=getSession();
        return (List<String>) session.getAttribute(EXPRESSIONS_IN_SESSION);
    }



}
